<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商名称" prop="name" label-width="100px">
        <el-input
          v-model="queryParams.name"
          placeholder="供应商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金蝶编号" prop="kingdeenum">
        <el-input
          v-model="queryParams.kingdeenum"
          placeholder="请输入金蝶编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="同步状态" prop="synchstat">
        <el-select v-model="queryParams.synchstat" placeholder="请选择同步状态" clearable size="small">
          <el-option
            v-for="dict in synchstatOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactstel">
        <el-input
          v-model="queryParams.contactstel"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="供应商类型" prop="type" label-width="100px">
        <el-select v-model="queryParams.type" placeholder="请选择供应商类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="美团编号" prop="mtCustCode">
        <el-input v-model="queryParams.mtCustCode" size="small" placeholder="请输入美团编号" clearable @keyup.erter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="mini"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['organization:supplier:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['organization:supplier:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['organization:supplier:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['organization:supplier:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat" width="80"/>
<!--      <el-table-column label="联系人" align="center" prop="contacts" />-->
      <el-table-column label="联系电话" align="center" prop="contactstel" width="100"/>
      <!-- <el-table-column label="关联用户" align="center" prop="realName" />
      <el-table-column label="关联组织" align="center" prop="orgname" /> -->
      <el-table-column label="金蝶编号" align="center" prop="kingdeenum" width="100"/>
      <el-table-column label="美团编码" align="center" prop="mtCustCode" width="100"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" width="60"/>
      <el-table-column label="关联用户/组织" align="center" prop="realName" >
          <template slot-scope="scope">
          <span v-if="scope.row.type == 1">{{ scope.row.realName }}</span>
          <span v-if="scope.row.type == 3">{{ scope.row.orgname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createByName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="140"/>

      <!-- <el-table-column label="同步日期" align="center" prop="synchtime" width="180">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.synchtime, '{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
    <el-table-column label="同步状态" align="center" prop="synchstat" :formatter="synchstatFormat" />
    <el-table-column label="同步信息" align="center" prop="synchinfo" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['organization:supplier:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['organization:supplier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="120px">
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入供应商名字" clearable />
        </el-form-item>
        <el-form-item label="简称" prop="shortname">
          <el-input v-model="form.shortname" placeholder="请输入供应商简称" clearable />
        </el-form-item>
        <el-form-item label="供应商类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择供应商类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
              @click.native="getType($event, dict)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联用户" prop="userid" v-show="userShow">
          <!-- <el-input v-model="form.userid" placeholder="请输入关联用户id" /> -->
          <el-select v-model="form.userid" placeholder="请选择用户" filterable clearable size="small">
            <el-option
              v-for="item in userOptions"
              :key="item.guid"
              :label="item.realname"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="关联组织" prop="orgguid" v-show="orgShow">
          <!-- <el-input v-model="form.userid" placeholder="请输入关联用户id" /> -->
          <el-select v-model="form.orgguid" placeholder="请选择组织" filterable clearable size="small">
            <el-option
              v-for="item in orgOptions"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开户行" prop="bank">
          <el-input v-model="form.bank" placeholder="请输入开户行名字" clearable />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankcode">
          <el-input v-model="form.bankcode" placeholder="请输入银行卡号" clearable />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" clearable />
        </el-form-item>
        <!-- <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="district">
          <el-input v-model="form.district" placeholder="请输入区" />
        </el-form-item> -->
        <el-form-item label="地址" prop="province">
          <!-- <el-cascader size="large" :options="options" v-model="form.add"  @change="handleChange"> </el-cascader> -->
          <v-distpicker :province="form.province" :city="form.city" :area="form.district" @selected="onSelected"></v-distpicker>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" clearable />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactstel">
          <el-input v-model="form.contactstel" placeholder="请输入联系电话" clearable />
        </el-form-item>
        <el-form-item label="美团编码" prop="mtCode">
          <el-input v-model="form.mtCode" placeholder="请输入美团编码" clearable/>
        </el-form-item>
        <el-form-item label="金蝶编号" prop="kingdeenum">
          <el-input v-model="form.kingdeenum" placeholder="请输入金蝶编号" clearable />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <!-- <el-input v-model="form.userid" placeholder="请输入关联用户id" /> -->
          <el-select v-model="form.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier, exportSupplier } from "@/api/organization/supplier";
import { tuserList, findOrganizationList } from "@/api/public";

export default {
  name: "Supplier",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 同步状态字典
      synchstatOptions: [],
      // 状态字典
      statusOptions: [],
      // 1-种植户 2-其他 3-内部供应商字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        kingdeenum: null,
        synchstat: null,
        status: null,
        contacts: null,
        contactstel: null,
        type: null,
        mtCustCode:null,
      },
      // 表单参数
      form: {
        orgguid:''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "供应商名字不能为空", trigger: "blur" }
        ],
        shortname: [
          { required: true, message: "供应商简称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "供应商类型不能为空", trigger: "change" }
        ],
        // contacts: [
        //   { required: true, message: "联系人不能为空", trigger: "blur" }
        // ],
        // contactstel: [
        //   { required: true, message: "联系电话不能为空", trigger: "blur" }
        // ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      },
      userOptions: [],
      //是否显示关联用户
      userShow : false,
      //是否显示关联组织
      orgShow: false,
      orgOptions : [],
      dateRange: [],
    };
  },
  created() {
    this.getList();
    this.userList();
    this.findOrganizationList();
    this.getDicts("sync_status").then(response => {
      this.synchstatOptions = response.data;
    });
    this.getDicts("general_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("supplier_type").then(response => {
      this.typeOptions = response.data;
    });
    //初始化下单时间
    // let firDate = this.getNowMonthDate().join('-');
    // let curDate = this.getDate(0).join('-');
    // this.dateRange = [firDate, curDate];
  },
  methods: {
    //获取当前日期
    getDate: function (dayCount = 0) {
      var dd = new Date();
      dd.setDate(dd.getDate()+dayCount);//设置日期
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1;//获取当前月份的日期
      var fullM = m.toString().length == 2 ? m : '0' + m;
      var d = dd.getDate();
      // return y+"-"+fullM+"-"+d;
      return [y, fullM, d];
    },
    //获取本月第一天
    getNowMonthDate: function () {
      var date = new Date();
      var year = date.getFullYear() + "";
      var month = date.getMonth() + 1 + "";
      // 本月第一天日期
      // var begin = year + "-" + month + "-01"
      return [year, month, "01"];
    },
    onSelected(data) {
      this.form.province = data.province.value
      this.form.city = data.city.value
      this.form.district = data.area.value
    },
    getType(event, item){
      let type = item.dictValue;
      console.log(type);
      if(type == 1){
        this.userShow = true;
        this.orgShow = false;
        this.form.orgguid='';
      }else if(type == 3){
        this.orgShow = true;
        this.userShow = false;
      }else{
        this.userShow = false;
        this.orgShow = false;
        this.form.orgguid='';
      }
    },
    //查询组织
    findOrganizationList(){
      findOrganizationList().then(response => {
        this.orgOptions = response.data;
      });
    },
    //查询用户
    userList(){
      tuserList().then(response => {
        this.userOptions = response.data.filter(item => item.guid);
      });
    },
    /** 查询供应商列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange);
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 同步状态字典翻译
    synchstatFormat(row, column) {
      return this.selectDictLabel(this.synchstatOptions, row.synchstat);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 1-种植户 2-其他 3-内部供应商字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        shortname: null,
        userid: null,
        kingdeenum: null,
        synchtime: null,
        synchstat: null,
        synchinfo: null,
        status: null,
        province: null,
        city: null,
        district: null,
        address: null,
        contacts: null,
        contactstel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        type: null,
        orgguid : null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupplier(id).then(response => {
        this.form = response.data;
        if(response.data.type == 1){
          this.userShow = true;
          this.orgShow = false;
        }else if(response.data.type == 3){
          this.orgShow = true;
          this.userShow = false;
        }else{
          this.userShow = false;
          this.orgShow = false;
        }
        this.open = true;
        this.title = "修改供应商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.type == 1){
        if(this.form.userid == null){
          this.msgError("请选择关联用户");
          return;
        }
      }else if(this.form.type == 3){
        if(this.form.orgguid == null){
          this.msgError("请选择关联组织");
          return;
        }
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSupplier(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除供应商编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupplier(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有供应商数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSupplier(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
